package com.ruoyi.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * FileName: Request2MapUtil
 * Author:   miuqin
 * Date:     2018/12/25 14:55
 * Description
 */
public class Request2MapUtil {

    private static Logger logger = LoggerFactory.getLogger(Request2MapUtil.class);

    public static Map<String,Object> wrapMap(HttpServletRequest request){
        Map<String,Object> params = new HashMap<>();
        String parameterName = null;
        String paramaterValue = null;

        Map<String,String[]> parameterMap = request.getParameterMap();
        try {
            Enumeration<String> enumeration = request.getParameterNames();
            while (enumeration.hasMoreElements()){
                parameterName = enumeration.nextElement();
                paramaterValue = request.getParameter(parameterName);
                if(paramaterValue.equals(new String(paramaterValue.getBytes("iso8859-1"), "iso8859-1")))
                {
                    paramaterValue=new String(request.getParameter(parameterName).getBytes("iso8859-1"),"utf-8");
                }
                params.put(parameterName,paramaterValue);
            }
        }catch (Exception e){
            logger.error("Request2MapUtil编码转换",e);
        }
        return params;
    }

}